package net.uman.web.front;

import java.util.Date;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import net.uman.entity.User;
import net.uman.framework.modules.security.springsecurity.SpringSecurityUtils;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.UserManager;
import net.uman.util.Constants;
import net.uman.web.CrudActionSupport;
@Namespace("/front")
public class MemberAction extends CrudActionSupport<User> {
	private static final long serialVersionUID = -8671429141191069828L;
	
	@Autowired
	private UserManager userManager;
	
	public String applyMember(){
		return "apply";
	}
	
	public void sureApply(){
		try{
			User newuser = new User();
			newuser.setLoginName(Struts2Utils.getParameter("loginName"));
			newuser.setName(Struts2Utils.getParameter("userName"));
			User e_user = userManager.getUserByLoginName(newuser.getLoginName());
			newuser.setAddDate(new Date(System.currentTimeMillis()));
			newuser.setEmail(Struts2Utils.getParameter("email"));
			if(e_user==null){
				String pwd = Struts2Utils.getParameter("password");
				newuser.setPassword(new Md5PasswordEncoder().encodePassword(pwd, newuser.getLoginName()));
				newuser.setPhone(Struts2Utils.getParameter("phone")!=null?Struts2Utils.getParameter("phone"):"");
				newuser.setPoint(0);
				newuser.setType(Constants.USER_TYPE_USER);
				userManager.save(newuser);
				Struts2Utils.renderText("注册用户完成！");
			}else{
				Struts2Utils.renderText("用户名或登录名已存在！");
			}
		}catch (Exception e) {
			e.printStackTrace();
			Struts2Utils.renderText("注册用户失败！");
		}
	}
	
	public void checkLoginName(){
		try {
			User newuser = userManager.getUserByLoginName(Struts2Utils.getParameter("loginName"));
			if(newuser==null)
				Struts2Utils.renderText("true");
			else
				Struts2Utils.renderText("用户名已存在！");
		} catch (Exception e) {
			e.printStackTrace();
			Struts2Utils.renderText("系统正忙请稍候再试！");
		}
	}
	
	public String login(){
		return "login";
	}
	
	public void checkUser(){
		try{
			Struts2Utils.getSession().removeAttribute("user");
			String userName = Struts2Utils.getParameter("loginName");
			String password = Struts2Utils.getParameter("password");
			User user_ck = userManager.getUserByLoginName(userName);
			if(user_ck != null){
				if(user_ck.encodePassword(password).equals(user_ck.getPassword())){
					Struts2Utils.setSessionAttribute("user", user_ck);
					Struts2Utils.renderText("success");
				}else{
					Struts2Utils.renderText("密码错误！");
				}
			}else{
				Struts2Utils.renderText("用户名不存在！");
			}
		}catch(Exception e){
			e.printStackTrace();
			Struts2Utils.renderText("系统忙，请稍候再试！");
		}
	}
	
	/**
	 * 多处引用，如过要修改，请和所有人员联系
	 */
	public void isLoginUser(){
		if(SpringSecurityUtils.getCurrentUser()!=null)
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
	}
	
	public User getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}

}
